<?php

class CommentController extends \Base\AbstractController {
    /**
     * @desc 朋友圈评论列表
     */
    public function listAction() {
        $request      = $this->getRequest();
        $class        = (int)$request->get('class');
        $keyword      = trim((string)$request->get('keyword'));
        $memberMapper = \Mapper\MemberModel::getInstance();
        $memberIds    = [];
        $return       = [];
        $where        = [];

        if(!empty($keyword) && !empty($class)) {

            switch($class) {
                case 1:
                    $first = ['from_uid' => $keyword];
                    break;

                case 2 :
                    $memberModels = $memberMapper->fetchAll(['nick_name LIKE "%' . $keyword . '%"']);

                    foreach($memberModels as $memberModel) {
                        if($memberModel instanceof \MemberModel) {
                            $memberIds[] = $memberModel->getId();
                        }
                    }

                    $first = ['from_uid' => $memberIds];
                    break;

                case 3 :
                    $first = ['cid' => $keyword];
                    break;

                default :
                    $first = [];
                    break;
            }
        }

        $second = ['type' => 1]; //指定朋友圈

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second);
        } elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = $first : $where = $second;
        }

        $page = new \Lib\Paginator('comment');
        $page->where($where);
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $this->assign('count', $page->count());
        $models = $page->lists();

        foreach($models as $model) {
            $data       = $model->toArray();
            $fromMember = $memberMapper->findById($data['from_uid']);

            if($fromMember instanceof \MemberModel) {
                $data['from_nick_name'] = urldecode($fromMember->getNick_name());
            }

            $return[] = $data;
        }

        $this->assign('lists', $return);
    }

    public function delAction() {

        $request      = $this->getRequest();
        $id           = $request->get('id');
        $status       = $request->get('status');
        $friendCircle = \Mapper\CommentModel::getInstance();

        if($status == 1) {
            $temp = 2;
        } elseif($status == 2) {
            $temp = 1;
        }

        try {
            $friendCircle->update(['status' => $temp], ['id' => $id]);
            return \Response::msg('操作成功');
        } catch(Exception $exc) {
            return \Response::errmsg($exc->getMessage());
        }
    }

}
